package com.itsv.xhyy;

import java.util.ArrayList;

import com.itsv.cyzx.adapter.TWNineAdapter;
import com.itsv.cyzx.bean.TWBean;
import com.itsv.cyzx.db.DingCanDB;
import com.itsv.cyzx.util.JsonUtil;
import com.itsv.cyzx.util.MD5;
import com.itsv.cyzx.util.StringUtil;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.DialogInterface.OnKeyListener;
import android.graphics.Color;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class DingCanActivity extends Activity implements OnItemClickListener,
		View.OnClickListener {

	private GridView gv;
	private ArrayList<TWBean> twBeans = new ArrayList<TWBean>();
	private ArrayList<TWBean> list = new ArrayList<TWBean>();
	private TWNineAdapter twAdapter;
	private Button backButton;
	private EditText numEditText;
	private int shuliang;
	private DingCanDB db;
	private Builder builder = null;
	private AlertDialog login_dialog = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_tw_nine);
		db = new DingCanDB(DingCanActivity.this);
		builder = new com.itsv.cyzx.view.HuzAlertDialog.Builder(this);
		initView();
		new GetDataTask().execute();

	}

	private void initView() {
		backButton = (Button) findViewById(R.id.twNine_ly_back);
		backButton.setOnClickListener(this);
		gv = (GridView) findViewById(R.id.twNine_gv);
		twAdapter = new TWNineAdapter(this, twBeans, gv);
		twAdapter.setData(twBeans);
		gv.setAdapter(twAdapter);
		gv.setOnItemClickListener(this);

		// 菊花动画效果
	}

	public void showLoginDialog(final TWBean mm) {
		final LayoutInflater factory = LayoutInflater.from(this);
		final View DialogView = factory.inflate(R.layout.dialog_dingcan, null);
		numEditText = (EditText) DialogView.findViewById(R.id.editText_num);
		builder.setTitle("请输入数量");
		builder.setView(DialogView);

		builder.setPositiveButton("确定", new OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				mm.setShuliang(numEditText.getText().toString());
				db.insertWWCList(mm);
				login_dialog.cancel();
				Toast.makeText(DingCanActivity.this, "已加入我的订单",
						Toast.LENGTH_LONG).show();

			}
		});
		builder.setNegativeButton("取消", new OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				login_dialog.cancel();

			}
		});
		login_dialog = builder.show();
	}

	// private void showchage(final TWBean mm) {
	// final AlertDialog dlg = new AlertDialog.Builder(this).create();
	// dlg.show();
	// Window window = dlg.getWindow();
	// LayoutInflater factory = LayoutInflater.from(this);
	// View dialogView = factory.inflate(R.layout.dialog_dingcan, null);
	// numEditText = (EditText) dialogView.findViewById(R.id.editText_num);
	// // *** 主要就是在这里实现这种效果的.
	// // 设置窗口的内容页面,shrew_exit_dialog.xml文件中定义view内容
	// Button jia = (Button) dialogView.findViewById(R.id.num_s);
	// Button jian = (Button) dialogView.findViewById(R.id.num_x);
	// String num = numEditText.getText().toString();
	// System.out.println("^^^^^^^^^^^^^^^^^^" + num);
	// shuliang = Integer.parseInt(num);
	// jia.setOnClickListener(new View.OnClickListener() {
	// public void onClick(View v) {
	// numEditText.setText(shuliang + 1);
	// }
	// });
	// jian.setOnClickListener(new View.OnClickListener() {
	// public void onClick(View v) {
	// if (shuliang > 1) {
	// numEditText.setText(shuliang - 1);
	// }
	// }
	// });
	// Button ok = (Button) dialogView.findViewById(R.id.dialog_yes);
	// // 获取时间
	// // 为确认按钮添加事件,执行退出应用操作
	// ok.setOnClickListener(new View.OnClickListener() {
	// public void onClick(View v) {
	// mm.setShuliang(numEditText.getText().toString());
	// db.insertWWCList(mm);
	// dlg.cancel();
	// }
	// });
	//
	// // 关闭alert对话框架
	// Button cancel = (Button) dialogView.findViewById(R.id.dialog_no);
	// cancel.setOnClickListener(new View.OnClickListener() {
	// public void onClick(View v) {
	// dlg.cancel();
	// }
	// });
	// // *** 主要就是在这里实现这种效果的.
	// // 设置窗口的内容页面,shrew_exit_dialog.xml文件中定义view内容
	// window.setContentView(dialogView);
	//
	// }

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		System.out.println("---------信件详情");
		showLoginDialog(list.get(arg2));

	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}

	private class GetDataTask extends AsyncTask<Void, Void, Integer> {
		private boolean flag = false;

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
		}

		@Override
		protected Integer doInBackground(Void... params) {
			try {
				// 获取我的文化旅游

				twBeans = JsonUtil.getTWBeanData(StringUtil.getStrForJson(
						"dc.js", DingCanActivity.this));
				flag = true;
			} catch (Exception e) {
				e.printStackTrace();
				flag = false;
			}
			return 1;
		}

		@Override
		protected void onPostExecute(Integer result) {
			super.onPostExecute(result);

			if (flag == true) {

				// ly.onLoadingComplete(1);
				if (twBeans != null && twBeans.size() > 0) {
					list.addAll(twBeans);
					// 显示数据
					twAdapter.setData(list);

					// gv.setAdapter(twAdapter);
					twAdapter.notifyDataSetChanged();
				}
			}
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.twNine_ly_back:
			DingCanActivity.this.finish();
			break;

		default:
			break;
		}

	}

}
